<template>
  <button
    class="custom-button"
    :class="[
      `custom-button--${type}`,
      `custom-button--${size}`,
      { 'custom-button--loading': loading },
      { 'custom-button--disabled': disabled }
    ]"
    :disabled="disabled || loading"
    @click="handleClick"
  >
    <div v-if="loading" class="custom-button__loading">
      <div class="custom-button__spinner"></div>
    </div>
    <span class="custom-button__text">
      <slot />
    </span>
  </button>
</template>

<script setup lang="ts">
interface Props {
  type?: 'primary' | 'secondary' | 'outline'
  size?: 'small' | 'medium' | 'large'
  loading?: boolean
  disabled?: boolean
}

interface Emits {
  (e: 'click', event: MouseEvent): void
}

const props = withDefaults(defineProps<Props>(), {
  type: 'primary',
  size: 'medium',
  loading: false,
  disabled: false
})

const emit = defineEmits<Emits>()

const handleClick = (event: MouseEvent) => {
  if (!props.disabled && !props.loading) {
    emit('click', event)
  }
}
</script>

<style scoped>
.custom-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  outline: none;
  user-select: none;
}

.custom-button:focus {
  outline: 2px solid rgba(25, 137, 250, 0.3);
  outline-offset: 2px;
}

.custom-button--small {
  height: 32px;
  padding: 0 16px;
  font-size: 12px;
}

.custom-button--medium {
  height: 40px;
  padding: 0 20px;
  font-size: 14px;
}

.custom-button--large {
  height: 48px;
  padding: 0 24px;
  font-size: 16px;
}

.custom-button--primary {
  background: linear-gradient(135deg, #1989fa, #36cfc9);
  color: #fff;
  box-shadow: 0 2px 8px rgba(25, 137, 250, 0.3);
}

.custom-button--primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(25, 137, 250, 0.4);
}

.custom-button--secondary {
  background-color: #f5f5f5;
  color: #666;
  border: 1px solid #e5e5e5;
}

.custom-button--secondary:hover {
  background-color: #e5e5e5;
}

.custom-button--outline {
  background-color: transparent;
  color: #1989fa;
  border: 1px solid #1989fa;
}

.custom-button--outline:hover {
  background-color: #1989fa;
  color: #fff;
}

.custom-button--loading {
  cursor: not-allowed;
}

.custom-button--disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.custom-button__loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.custom-button__spinner {
  width: 16px;
  height: 16px;
  border: 2px solid transparent;
  border-top: 2px solid currentColor;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.custom-button__text {
  transition: opacity 0.3s ease;
}

.custom-button--loading .custom-button__text {
  opacity: 0;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>
